//顺序表基本运算算法
#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef char ElemType;
typedef struct
{	ElemType data[MaxSize];		//存放顺序表元素
    int length;					//存放顺序表的长度
} SqList;						//顺序表的类型


void InitList(SqList *&L) {
    L = (SqList*)malloc(sizeof(SqList));
    L->length = 0;
}

void CreateList(SqList* &L,ElemType* a,int n) {
    for(int i = 0;i < n;i++) {
        L->data[i] = a[i];
    }
    L->length = n;
}

void DispList(SqList*L) {
    for(int i = 0;i < L->length;i++)printf("%c",L->data[i]);
    printf("\n");
}

int ListLength(SqList*L) {
    return L->length;
}

bool ListEmpty(SqList*L) {
    if(L->length)return false;
    return true;
}

int LocateElem(SqList*L,ElemType e) {
    for(int i=0;i<L->length;i++) {
        if(L->data[i]==e)return i+1;
    }
    return 0;
}

void ListInsert(SqList* &L,int i,ElemType e) {
    i--;
    L->length++;
    for(int j =L->length-1;j>i;j--) {
        L->data[j]=L->data[j-1];
    }
    L->data[i]=e;
}

void ListDelete(SqList* &L,int i,ElemType e) {
    i--;
    L->length--;
    for(int j = i;j<L->length;j++) {
        L -> data[j] = L->data[j+1];
    }
}
void DestroyList(SqList*L) {
    free(L);
}
int main() {
    SqList *L = NULL;
    char a[5] = {'a','b','c','d','e'};
    InitList(L);
    CreateList(L,a,5);
    DispList(L);
    printf("顺序表的长度为：%d\n",ListLength(L));
    printf("L是否为空：%s\n",ListEmpty(L)?"true":"fasle");
    printf("L中的第4个元素是：%c\n",L->data[3]);
    printf("元素b的位置是：%d\n",LocateElem(L,'b'));
    ListInsert(L,2,'f');
    DispList(L);
    ListDelete(L,3,L->data[2]);
    DispList(L);
    DestroyList(L);
    return 0;
}